package king.dominic.jlibrary.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;

import king.dominic.jlibrary.R;

/**
 * Created by king on 2016/5/16.
 * GuidePage
 */
public class GuidePage extends LinearLayout{
    private final ViewPager viewPager;
    private final LinearLayout dotParent;
    private float dotDistance;
    private boolean dotVisibility;
    private float dotMarginBottom;
    private int dotUnselected;
    private int dotSelected;
    private int count;
    private List<ImageView> dots;

    public GuidePage(Context context) {
        this(context,null);
    }

    public GuidePage(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view=View.inflate(context, R.layout.layout_guidepage,null);
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        addView(view);
        layoutParams.width=layoutParams.height=ViewGroup.LayoutParams.MATCH_PARENT;
        view.setLayoutParams(layoutParams);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager);
        dotParent=(LinearLayout)view.findViewById(R.id.dotParent);
        if(attrs==null)
            return;
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.GuidePage);
        dotSelected=typedArray.getResourceId(R.styleable.GuidePage_gp_dotSelectedSrc,0);
        dotUnselected=typedArray.getResourceId(R.styleable.GuidePage_gp_dotUnselectedSrc,0);
        dotMarginBottom = typedArray.getDimension(R.styleable.GuidePage_gp_dotMarginBottom, 0);
        dotVisibility=typedArray.getBoolean(R.styleable.GuidePage_gp_dotVisibility,true);
        dotDistance = typedArray.getDimension(R.styleable.GuidePage_gp_dotDistance, WindowUtil.dip2px(context,3));
        typedArray.recycle();
        setDotMarginBottom(dotMarginBottom);
        setDotVisibility(dotVisibility);
    }

    public void setDotDistance(float dotDistance) {
        this.dotDistance = dotDistance;
    }

    public void setDotVisibility(boolean dotVisibility) {
        this.dotVisibility = dotVisibility;
        if(dotVisibility)
            dotParent.setVisibility(View.VISIBLE);
        else
            dotParent.setVisibility(View.GONE);
    }

    public void setDotMarginBottom(float dotMarginBottom) {
        this.dotMarginBottom = dotMarginBottom;
        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) dotParent.getLayoutParams();
        lp.bottomMargin= (int) dotMarginBottom;
        dotParent.setLayoutParams(lp);
    }

    public void setDotUnselected(int dotUnselected) {
        this.dotUnselected = dotUnselected;
    }

    public void setDotSelected(int dotSelected) {
        this.dotSelected = dotSelected;
    }

    public void addImages(List<Integer> resIds){
        this.count=resIds.size();
        onCreateDots();
        initViewPager(resIds);
    }
    private void initViewPager(List<Integer> resIds){
        viewPager.setAdapter(new ImagePagerAdapter(getContext(),resIds));
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                selectedDot(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private void selectedDot(int index){
        if(dots==null  )
            return;
        if(index<0 || index>count)
            throw new IndexOutOfBoundsException("index="+index+" size="+count);
        for(ImageView dot:dots){
            dot.setImageResource(dotUnselected);
        }
        ImageView image=dots.get(index);
        image.setImageResource(dotSelected);
    }
    protected void onCreateDots(){
        dots=new ArrayList<>();
        dots.clear();
        int padding= (int) (dotDistance/2);
        for(int i=0;i<count;i++){
            ImageView image = new ImageView(getContext());
            image.setPadding(padding,padding,padding,padding);
            dotParent.addView(image);
            dots.add(image);
        }
        selectedDot(0);
    }
    public Button addButton(float x, float y, int w, int h, int backgroundResource, OnClickListener l){
        Button b=new Button(getContext());
        return b;
    }
}
